/**   
 * @Title: BindMobile_Bind.java 
 * @Package： com.esun.rabbit2.viewer.mainact.mylotteryact.bindmobile 
 * @Description: 已绑定过手机号
 * @Author Miko baoz@500wan.com   
 * @Date 2011-7-3 下午06:24:28  
 * Copyright (C) 2011 Shenzhen E-sun Sky Net Technology Co.,ltd.
 */
package com.esun.rabbit2.viewer.mainact.mylotteryact.bindmobile;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

import com.esun.config.Language;
import com.esun.network.ConnectionChangeReceiver;
import com.esun.rabbit2.communicaters.myinfo.BindMobileChangeCommunicater;
import com.esun.ui.R;
import com.esun.util.EditTextInputUitl;
import com.esun.util.ViewerUtil;

/**
 * @ClassName: BindMobile_Bind
 * @Description: 已绑定过手机号Activity
 * @author Miko baoz@500wan.com
 * @date 2011-7-3 下午06:24:28
 * 
 */
public class BindMobile_Bind extends Activity implements OnClickListener {

	private EditText etOld;
	private Button btnOldClear;

	private EditText etNew;
	private Button btnNewClear;

	private ImageButton btnGoBack;
	private Button btnSubmit;

	private String strOld;
	private String strNew;

	private ProgressDialog progress;

	private BindMobileChangeCommunicater communicater = new BindMobileChangeCommunicater();

	private Handler handler;

	private static final int GET_CODE_SUCCESS = 100001;
	private static final int GET_CODE_FAILED = 1000002;

	private static final int SEND_CODE_SUCCESS = 100003;
	private static final int SEND_CODE_FAILED = 100004;

	private Dialog dialogInputCode;

	public static boolean isBindOK = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.bind_mobile_bind);
		init();
		initHandler();
	}

	private void init() {
		etOld = (EditText) this.findViewById(R.id.bind_mobile_bind_et_numOld);
		btnOldClear = (Button) this.findViewById(R.id.bind_mobile_bind_et_numOld_btn_claen);
		etOld = new EditTextInputUitl(etOld, btnOldClear).getEditText();

		etNew = (EditText) this.findViewById(R.id.bind_mobile_bind_et_numNew);
		btnNewClear = (Button) this.findViewById(R.id.bind_mobile_bind_et_numNew_btn_claen);
		etNew = new EditTextInputUitl(etNew, btnNewClear).getEditText();

		btnGoBack = (ImageButton) this.findViewById(R.id.bind_mobile_bind_btn_goBack);
		btnGoBack.setOnClickListener(this);

		btnSubmit = (Button) this.findViewById(R.id.bind_mobile_bind_btn_submit);
		btnSubmit.setOnClickListener(this);

		progress = ViewerUtil.showProgressDialog(this, "请稍候...");
	}

	private void initHandler() {
		handler = new Handler() {

			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);

				progress.dismiss();
				switch (msg.what) {
				case GET_CODE_SUCCESS:

					startActivity(new Intent().setClass(BindMobile_Bind.this, BindMobile_InputCodeAct.class).putExtra("new", strNew));
					// showInputCodeDialog();

					break;

				case GET_CODE_FAILED:

					ViewerUtil.showTipDialog(BindMobile_Bind.this, Language.BINDING_MOBEL_FAIL, false);
					// Toast.makeText(BindMobile_Bind.this, Language.BINDING_MOBEL_FAIL, 0).show();
					break;

				case SEND_CODE_SUCCESS:

					dialogInputCode.dismiss();
					ViewerUtil.showTipDialog(BindMobile_Bind.this, "手机号码修改成功！", true);

					break;

				case SEND_CODE_FAILED:

					ViewerUtil.showTipDialog(BindMobile_Bind.this, "验证码错误，请重新输入", false);

					break;

				default:
					break;
				}
			}
		};
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.bind_mobile_bind_btn_goBack:
			this.finish();
			break;

		case R.id.bind_mobile_bind_btn_submit:

			if (checkInput()) {
				progress.show();
				startGetVerifyCode();
			}

			break;

		default:
			break;
		}
	}

	private boolean checkInput() {

		strOld = etOld.getText().toString();
		strNew = etNew.getText().toString();

		if (strOld.length() < 1) {
			ViewerUtil.showTipDialog(this, "请正确输入手机号", false);
			return false;
		} else if (strNew.length() < 1) {
			ViewerUtil.showTipDialog(this, "请正确输入手机号", false);
			return false;
		} else if (strOld.equals(strNew)) {
			ViewerUtil.showTipDialog(this, "请正确输入手机号", false);
			return false;
		} else {
			return true;
		}
	}

	private void startGetVerifyCode() {
		new Thread() {

			@Override
			public void run() {
				super.run();
				getVerifyCode();
			}
		}.start();
	}

	private void getVerifyCode() {

		if (communicater.getVerifyCode(strOld, strNew)) {
			Message msg = handler.obtainMessage(GET_CODE_SUCCESS);
			handler.sendMessage(msg);
		} else {
			Message msg = handler.obtainMessage(GET_CODE_FAILED);
			handler.sendMessage(msg);
		}
	}

	@Override
	protected void onResume() {
		super.onResume();

		if (isBindOK) {
			this.finish();
		}
		this.registerReceiver(ConnectionChangeReceiver.getConnectionChangeReceiver(), ConnectionChangeReceiver.getIntentFilter());
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		this.unregisterReceiver(ConnectionChangeReceiver.getConnectionChangeReceiver());
	}
}
